<?php
/**
 * 工作流
 * Enter description here ...
 * @author cc
 * flow_type_list  新建工作分类列表
 * work_add 新建工作流程
 * work_name_insert 新建工作
 * work_list   待办理工作流程列表
 * work_form_index 查询工作表单
 * work_form_view 工作表单视图
 * work_form_control 保存、提交工作表单
 * work_form_data 保存表单的数据
 * work_next 选择下一步
 * work_next_ok 保存下一步办理人员
 * work_proc_user  查询流程设置该步骤的经办人员
 * work_proc_dept 查询流程设置该步骤的经办部门
 * work_select 工作流程查询
 * work_select_type_list 工作流程查询分类列表
 * work_select_list 工作流程查询结果
 * work_delete   删除工作流
 * work_proc_view  查看工作的流程图
 * work_form_look 工作流表单查看
 * replace   替换表单部分数据
 * work_search  工作流程监控
 * work_search_type_list  工作流程监控分类列表
 * work_search_list  工作流程监控查询结果
 * mywork  我的工作流程
 * mywork_type_list  我的工作流程分类列表
 * mywork_list  我的工作流程查询结果
 * 
 * 
 */
namespace Home\Controller;
use Think\Controller;
class WorkFlowController extends CommonController{
	public function way(){
		session("action","WorkFlowSys");
		session("menu_id",I('get.menu_id'));
		$this->redirect("Public/index");
	}
	
	public function index(){
		$this->redirect("work_new");
	}
	
	public function create_index(){//创建索引
//		CREATE INDEX index_name ON oa_workflow_work_prcs (proc_user);
//		CREATE INDEX index_name_checktime ON oa_checkinout (checktime);
//		CREATE INDEX index_name_userid ON oa_checkinout (user_id);
//		CREATE INDEX index_name_username ON oa_workflow_work (work_user_name);
//		CREATE INDEX index_name_typeid ON oa_workflow_work (type_id);
//		CREATE INDEX index_name_worktime ON oa_workflow_work (work_time);
//		CREATE INDEX index_name_prcsid ON oa_workflow_work_prcs (prcs_id);
//		CREATE INDEX index_name_workstatus ON oa_workflow_work (work_status);
//		CREATE INDEX index_name_workid ON oa_workflow_work (work_id);
//		CREATE INDEX index_name_workid ON oa_workflow_work_prcs (work_id);
//		CREATE INDEX index_name_procno ON oa_workflow_work_prcs (proc_no);
//		CREATE INDEX index_name_procuser ON oa_workflow_work_prcs (proc_user);
//		CREATE INDEX index_name_typeid ON oa_workflow_process (type_id);
//		CREATE INDEX index_name_procno ON oa_workflow_process (proc_no);
		
		
	}
	
	public function work_new(){
		$this->display();
	}
	
	public function flow_type_list(){
		$workflow_type = M("workflow_type");
	 	$workflow_sort = M("workflow_sort");
	 	$data_sort = $workflow_sort->order('sort_no')->select();
	 	$data = array();
	 	foreach ($data_sort as $key=>$value){//根据分类查询
	 		$sort_id = $value['sort_id'];
	 		$data_type = $workflow_type->where('sort_id='.$sort_id." and status = 1")->select();
	 		foreach ($data_type as $key=>$value2){
	 			$value['one'][$key] = $value2;
	 		}
	 		$data[] = $value;
	 	}
	 	$this->assign('data',$data);
	 	$this->display();
	}
	
	public function work_add(){
		$time = date('Y-m-d H:i:s');
		
		$workflow_type = M("workflow_type");
		$sort_id = I('get.sort_id') ? I('get.sort_id') : 1;
		$type_id = I('get.type_id') ? I('get.type_id') : 1;
		
		if(!is_id($sort_id) || !is_id($type_id)){ $this->error("非法参数！"); }
		
		$type_name = $workflow_type->where('type_id=%d',$type_id)->getField('type_name');
		$type_explain = $workflow_type->where('type_id=%d',$type_id)->getField('type_explain');
		$this->assign('type_name',$type_name);
		$this->assign('name',session('name'));
		$this->assign('time',$time);
		$this->assign('type_explain',$type_explain);
		$this->assign('type_id',$type_id);
		$this->display();
	}
	
	public function type_explain(){
		$workflow_type = M("workflow_type");
		$type_id = I('get.type_id');
		echo $workflow_type->where('type_id=%d',$type_id)->getField('type_explain');
	}
	
	public function work_name_insert(){
		$data = array(
			'work_name'=>$_POST['work_name'],
			'type_id'=>$_POST['type_id'],
			'work_time'=>$_POST['work_time'],
			'work_status'=>'1',
			'work_author'=>session('name'),
			'work_user_name'=>session('user_name')
		);
		$data1 = array(
			'proc_no'=>'1',
			'proc_user'=>session('user_name'),
			'prcs_flag'=>'1'
		);
		$workflow_work = M('workflow_work');
		$workflow_work_prcs = M('workflow_work_prcs');
		$workflow_work_data = M('workflow_work_data');
		$success = $workflow_work->add($data);
		if($success){//当新的工作流创建成功时，把该工作流的状态添加到Oa_workflow_work_prcs表中
			$data1['work_id'] = $success;
			$data2['work_id'] = $success;
			$workflow_work_prcs->add($data1);
			$workflow_work_data->add($data2);
	 		$this->systemlog('add', '工作流', '添加工作流---'.$_POST['work_name']);
	 		echo "<script language='javascript'>parent.location='work_list';</script>";exit;
//	 		$this->success('添加工作流成功','work_list');
	 	}else{
	 		$this->error('添加工作流失败');
	 	}
	}
	
	public function work_list(){
	 	$user_name = session('user_name');
		$model = M();
		$sql = "select a.prcs_id, c.proc_id, a.prcs_flag, d.work_id,d.work_name, d.type_id from oa_workflow_work_prcs a, oa_workflow_work d, oa_workflow_process c where d.work_status=1 and a.work_id=d.work_id and d.type_id=c.type_id and a.proc_no=c.proc_no and a.proc_user='$user_name' and a.proc_no IN (select MAX(proc_no) FROM oa_workflow_work_prcs b WHERE b.work_id=a.work_id ) GROUP BY a.work_id ORDER BY a.work_id DESC";
//$sql = "select a.prcs_id, a.proc_no, a.prcs_flag, d.work_id,d.work_name, d.type_id from oa_workflow_work_prcs a, oa_workflow_work d where d.work_status=1 and a.work_id=d.work_id and a.proc_user='$user_name' and a.proc_no IN (select MAX(proc_no) FROM oa_workflow_work_prcs b WHERE b.work_id=a.work_id ) GROUP BY a.work_id ORDER BY a.work_id DESC";
		$data = $model->query($sql);
		$this->assign('data',$data);
		$this->display();
	}
	
	public function work_form_index(){//办理工作表单
		$id['prcs_id'] = I('get.prcs_id');
	 	$id['proc_id'] = I('get.proc_id');
		$id['type_id'] = I('get.type_id');
	 	$id['work_id'] = I('get.work_id');
	 	
	 	$workflow_work_prcs = M('workflow_work_prcs');//工作流状态表
	 	$data_prcs = $workflow_work_prcs->where('prcs_id=%d',$id['prcs_id'])->find();
	 	if($data_prcs['user_name'] == null){
		 	$this->assign('id',$id);
		 	$this->display();
	 	}else{
	 		$user_name = $data_prcs['user_name'];
	 		$user = M("sys_user");
	 		$name = $user->where("user_name = '$user_name'")->getField('name');
	 		$this->error('对不起，该工作流已于<font color=red>'.$data_prcs['prcs_time'].'</font>被<font color=red>'.$name.'</font>办理');
	 	}
	}
	
	public function work_form_view(){
		$id['prcs_id'] = I('get.prcs_id');
	 	$id['proc_id'] = I('get.proc_id');
		$id['type_id'] = I('get.type_id');
	 	$id['work_id'] = I('get.work_id');
	 	if(!is_id($id['prcs_id']) || !is_id($id['proc_id']) || !is_id($id['type_id']) || !is_id($id['work_id'])){ $this->error("非法参数！"); }
		session('wpsqqd', '');
//	 	dump($id);
	 	$workflow_form = M("workflow_form");//流程表单表
	 	$workflow_type = M("workflow_type");//工作流程表
	 	$workflow_work = M('workflow_work');//工作流表
	 	$workflow_proc = M("workflow_process");//流程步骤表
		$workflow_work_prcs = M('workflow_work_prcs');//工作流状态表
		$workflow_work_data = M('workflow_work_data');//存储工作流的值
		
		$prcs_flag = $workflow_work_prcs->where('prcs_id=%d',$id['prcs_id'])->getField('prcs_flag');
		if($prcs_flag == 1){
		 	$data_prcs['prcs_flag'] = '2';
		 	$workflow_work_prcs->where('prcs_id=%d',$id['prcs_id'])->save($data_prcs); //将工作流接收状态的值改成2，表示该工作流已接收
		}
		$data_type = $workflow_type->where('type_id=%d',$id['type_id'])->find();
		$data_form = $workflow_form->where('form_id=%d',$data_type['form_id'])->find();
		$id['form_id'] = $data_form['form_id'];
		$data_proc = $workflow_proc->where('proc_id=%d',$id['proc_id'])->find();
		$proc_item = explode(',', $data_proc['proc_item']);
		$form_model = html_element($data_form['form_model']);
		$data_value3 = $workflow_work_data->where('work_id=%d',$id['work_id'])->getField('data_value');//查询出的表单值
		if($data_value3 == null){
			$data_value = array();
		}else{
			$data_value = explode("***", $data_value3);
		}
//		dump($data_value);exit;
		foreach ($data_value as $value1){
			if($value1 == null){//当表单中提交的值有空的
				$data_value1[] = " ";
			}else{
				$data_value1[] = $value1;
			}
		}
		
		$i=0;//取出当前步骤第一个可写字段在表单中之前的字段总和。
		foreach ($form_model as $value1){
			$title = explode(" ",strstr($value1, 'title='));
	 		$form_item = substr($title[0], '6');//表title的值
	 		if($proc_item[0] == $form_item){
	 			break;
	 		}else{
		 		$i++;
	 		}
		}
//		echo $i;
		if($i != 0){//表示当前步骤之前还有值
			for($j=$i;$j<(count($data_value));$j++){
				$data_value2[] = $data_value[$j];
			}
		}else{
			$data_value2 = $data_value;
		}
		
	 	foreach ($form_model as $key=>$value){
	 		$title = explode(" ",strstr($value, 'title='));
	 		$form_item = substr($title[0], '6');//表title的值
	 		if(!in_array($form_item, $proc_item)){//不可写
	 			$data_bak1 = array(
	 				$value => '<font color=red>'.$data_value1[$key].'</font>',
	 			);
	 			$data_form['form_model'] = strtr($data_form['form_model'],$data_bak1);
	 		}else{
	 			$tihuan = 1;
	 		}
	 	}
	 	if($tihuan == 1){//开始替换
			$pattern="/((<{1})INPUT.*? title=.*?(>{1}))|(<{1}TEXTAREA.*? title=.*?(>{1}).*(<\/TEXTAREA>))|(<{1}SELECT.*? title=.*?(>{1}).*(<\/SELECT>))/";//正则模式
			preg_match_all($pattern,$data_form['form_model'],$m);
			$data_form['form_model'] = $this->replace($m[0], $data_value2, $data_form['form_model']);
	 	}
	 	$this->assign('id',$id);
		$this->assign('data_form',$data_form);
		$this->display();
	}

	public function work_form_control(){
		$this->display();
	}
	
	public function work_form_data(){
		$workflow_form = M("workflow_form");//流程表单表
	 	$workflow_type = M("workflow_type");//工作流程表
	 	$workflow_work = M('workflow_work');//工作流表
	 	$workflow_proc = M("workflow_process");//流程步骤表
		$workflow_work_prcs = M('workflow_work_prcs');//工作流状态表
		$workflow_work_data = M('workflow_work_data');//存储工作流的值
		
		$id = array(
			'prcs_id' => I('post.prcs_id'),
			'proc_id' => I('post.proc_id'),
			'type_id' => I('post.type_id'),
			'work_id' => I('post.work_id'),
			'form_id' => I('post.form_id')
		);
	 	if(!is_id($id['prcs_id']) || !is_id($id['proc_id']) || !is_id($id['type_id']) || !is_id($id['work_id']) || !is_id($id['form_id'])){ $this->error("非法参数！"); }
		
		$data_prcs = $workflow_work_prcs->where('prcs_id=%d',$id['prcs_id'])->find();
		if($data_prcs['user_name'] != null){
			$user = M("sys_user");
			$user_name = $data_prcs['user_name'];
	 		$name = $user->where("user_name = '$user_name'")->getField('name');
			$this->error('对不起，该工作流已于<font color=red>'.$data_prcs['prcs_time'].'</font>被<font color=red>'.$name.'</font>办理','work_list');
		}
		$save_flag = $_POST['save_flag'];
		$proc_item = $workflow_proc->where('proc_id=%d',$id['proc_id'])->getField('proc_item');
		$proc_item = explode(",", $proc_item);
		$form_model = $workflow_form->where('form_id=%d',$id['form_id'])->getField('form_model');//查询表单模型
		$form_model = html_element($form_model);
//		dump($form_model);
		$data_value = $workflow_work_data->where('work_id=%d',$id['work_id'])->getField('data_value');
		$i=0;//取出当前步骤第一个可写字段在表单中之前的字段总和。
		foreach ($form_model as $value1){
			$title = explode(" ",strstr($value1, 'title='));
	 		$form_item = substr($title[0], '6');//表title的值
	 		if($proc_item[0] == $form_item){
	 			break;
	 		}else{
		 		$i++;
	 		}
		}
		$count = count($_POST)-6;//此处减去表单其他的值，只保留data_部分的数据
		if($count == 1){
			$data['data_value'] = $_POST['data_1'];
		}else{
			for($z=1;$z<=$count;$z++){
				$data_value3[] = $_POST['data_'.$z];
			}
			$data['data_value'] = implode('***', $data_value3);//将获取的表单值以***切割成字符串保存
		}
		if($i != 0){//表示当前步骤之前还有值
			$data_value = explode('***', $data_value);
			for($j=0;$j<$i;$j++){
				$data_value2[] = $data_value[$j];
			}
			$data_value2 = implode('***', $data_value2);
			$data['data_value'] = $data_value2."***".$data['data_value'];
		}
//		dump($data);exit;
		
		$workflow_work_data->where('work_id=%d',$id['work_id'])->save($data);
		if( session('wpsqqd') != ''){//销毁选择物品清单的session，此session在Administrator控制器下面的supplies_use_insert方法生成的
			session('wpsqqd', "");
		}
		if($save_flag == 1){//保存但不进入下一步
			echo "<script>parent.location='work_list';</script>";
		}elseif($save_flag == 2){//保存并下一步
			echo "<script>parent.location='work_next?prcs_id=".$id['prcs_id']."&proc_id=".$id['proc_id']."&type_id=".$id['type_id']."&work_id=".$id['work_id']."';</script>";
		}
	}
	
	public function work_next(){//选择经办人
		$time = date('Y-m-d H:i:s');
		$id['prcs_id'] = I('get.prcs_id');
	 	$id['proc_id'] = I('get.proc_id');
		$id['type_id'] = I('get.type_id');
	 	$id['work_id'] = I('get.work_id');
	 	if(!is_id($id['prcs_id']) || !is_id($id['proc_id']) || !is_id($id['type_id']) || !is_id($id['work_id'])){ $this->error("非法参数！"); }
	 	
	 	$user = M("sys_user");
	 	$dept = M('sys_dept');
	 	$workflow_form = M("workflow_form");//流程表单表
	 	$workflow_type = M("workflow_type");//工作流程表
	 	$workflow_work = M('workflow_work');//工作流表
	 	$workflow_proc = M("workflow_process");//流程步骤表
		$workflow_work_prcs = M('workflow_work_prcs');//工作流状态表
		$workflow_work_data = M('workflow_work_data');//存储工作流的值
		$information = M("information");//信息提示
		
		//判断审核的内容是否是“否决”，是则流程结束
		$data_value4 = $workflow_work_data->where('work_id=%d',$id['work_id'])->getField('data_value');
		if($data_value4 != null){
			$data_value = explode('***', $data_value4);
		}
		foreach ($data_value as $value){
			if($value == '否决'){
				$data_prcs4 = array(
					'user_name' => session('user_name'),
					'prcs_time' => $time
				);
				$workflow_work_prcs->where('prcs_id=%d',$id['prcs_id'])->save($data_prcs4);
				$data_prcs3 = array(
					'work_id' => $id['work_id'],
					'proc_no' => 0,
					'prcs_flag' => '1'
				);
				$workflow_work_prcs->add($data_prcs3);
				$data_work = array(
					'work_status' => 0,
				);
				$workflow_work->where('work_id=%d',$id['work_id'])->save($data_work);
				$data1 = $workflow_work->where('work_id=%d',$id['work_id'])->find();
				$data2 = array(
					'title' => $data1['work_name'],
					'content' => $data1['work_name']."  工作流于  ".$time." 被  ".session('name')."  驳回，流程于此结束，请注意查看",
					'url' => 'http://'.$_SERVER['HTTP_HOST'].__APP__.'/WorkFlow/work_select',
					'time' => $time,
					'status' => '1',
					'user_name' => $data1['work_user_name']
				);
				$information->add($data2);
				$this->systemlog("否决", "工作流程", "否决工作流程---".$data1['work_name']);
				$this->success('您已否决该工作流，该工作流程被返回！','work_list');
				exit;
				break;
			}
		}
		
		$form_id = $workflow_type->where('type_id=%d',$id['type_id'])->getField('form_id');
		$form_model = $workflow_form->where('form_id=%d',$form_id)->getField('form_model');//查询表单模型
		$form_model = html_element($form_model);
		$data_proc = $workflow_proc->where('proc_id=%d',$id['proc_id'])->find();//当前步骤
		if($data_proc['proc_to'] == 0){
			$data_prcs4 = array(
				'user_name' => session('user_name'),
				'prcs_time' => $time
			);
			$workflow_work_prcs->where('prcs_id=%d',$id['prcs_id'])->save($data_prcs4);
			$data_prcs3 = array(
				'work_id' => $id['work_id'],
				'proc_no' => 0,
				'prcs_flag' => '1'
			);
			$workflow_work_prcs->add($data_prcs3);
				$data_work = array(
					'work_status' => 0,
				);
				$workflow_work->where('work_id=%d',$id['work_id'])->save($data_work);
			$this->redirect('work_list');
			exit;
		}
		
		//查询出办理的人员，包括申请人
		$sql = "select proc_user from oa_workflow_work_prcs where work_id=".$id['work_id'];
		$model = M();
		$data_proc_user = $model->query($sql);
		if(!empty($data_proc_user['0']['proc_user'])){//当办理人员不为空的时候
			foreach ($data_proc_user as $key3=>$value3){
				$proc_user2[$key3] = $value3['proc_user'];
			}
			//添加当前步骤的办理人姓名
		}else{//为空的时候表示流程才申请
			$proc_user2[] = session('user_name');//吧申请人添加到办理人员数组中
		}
		
		$count = $workflow_proc->where('type_id=%d',$id['type_id'])->order('proc_no desc')->count();
		for($i=0;$i<$count;$i++){
			if($data_proc['proc_to'] != 0){//当下一步不为0时
				//查询出下一步的信息
				$data_proc1 = $workflow_proc->where('proc_no='.$data_proc['proc_to'].' AND type_id='.$id['type_id'])->find();//查询出下一步的内容
				//判断下一步的经办人员中是否有人在之前审核过该流程
				$proc_user3 = $data_proc1['proc_user'];//取出下一步的经办人员
				$proc_user4 = explode(',', $proc_user3);
				foreach ($proc_user4 as $value4){
					if(in_array($value4, $proc_user2)){
						$b = 1;//表示本次循环结束
						break;
					}else{
						$b = 0;//表示流程继续
					}
				}
//				if($b != 1){
					if($data_proc1['proc_in'] != null){//当转入条件不为空
						$proc_in = explode("\n", $data_proc1['proc_in']);
						foreach ($proc_in as $value1){
							$proc_in_one = explode("'", substr($value1,1,-1));//将转入的条件以'分隔
							foreach ($form_model as $key=>$value2){
						 		$title[] = explode(" ",strstr($value2, 'title='));
						 		$form_item = substr($title[$key][0], '6');//表title的值
						 		if($form_item == $proc_in_one[0]){//循环出条件的键
						 			$data_value2 = $data_value[$key];
									if($proc_in_one[1] == '>'){
							 			if($data_value2 > $proc_in_one[2]){
							 				$a = 1;
							 				break;
							 			}else{//条件不相等
							 				$a = 2;
							 			}
						 				$data_proc['proc_to'] = $data_proc1['proc_to'];
									}elseif($proc_in_one[1] == '<'){
										if($data_value2 < $proc_in_one[2]){
							 				$a = 1;
							 				break;
							 			}else{//条件不相等
							 				$a = 2;
							 			}
						 				$data_proc['proc_to'] = $data_proc1['proc_to'];
									}elseif($proc_in_one[1] == '<='){
										if($data_value2 <= $proc_in_one[2]){
							 				$a = 1;
							 				break;
							 			}else{//条件不相等
							 				$a = 2;
							 			}
						 				$data_proc['proc_to'] = $data_proc1['proc_to'];
									}elseif($proc_in_one[1] == '>='){
										if($data_value2 >= $proc_in_one[2]){
							 				$a = 1;
							 				break;
							 			}else{//条件不相等
							 				$a = 2;
							 			}
						 				$data_proc['proc_to'] = $data_proc1['proc_to'];
									}elseif($proc_in_one[1] == '!='){
										if($data_value2 != $proc_in_one[2]){
							 				$a = 1;
							 				break;
							 			}else{//条件不相等
							 				$a = 2;
							 			}
						 				$data_proc['proc_to'] = $data_proc1['proc_to'];
									}elseif($proc_in_one[1] == '='){
										if($data_value2 == $proc_in_one[2]){
							 				$a = 1;
							 				break;
							 			}else{//条件不相等
							 				$a = 2;
							 			}
						 				$data_proc['proc_to'] = $data_proc1['proc_to'];
									}
						 		}
						 		if($a == 1){
						 			break;
						 		}
						 	}
							if($a == 1){
					 			break;
					 		}
						}
					}else{//当转入条件为空
						if($data_proc1['proc_user'] != null){//当经办人员不为空
							$a=1;
						}elseif($data_proc['proc_to'] == 2 && empty($data_proc1['proc_in'])){//当经办人员为空
							$a=1;
						}
					}
//				}else{
//					$data_proc['proc_to'] = $data_proc1['proc_to'];
//				}
			}else{
				$data_prcs4 = array(
					'user_name' => session('user_name'),
					'prcs_time' => $time
				);
				$workflow_work_prcs->where('prcs_id=%d',$id['prcs_id'])->save($data_prcs3);
				$data_prcs3 = array(
					'work_id' => $id['work_id'],
					'proc_no' => 0,
					'prcs_flag' => '1'
				);
				$workflow_work_prcs->add($data_prcs3);
				$data_work = array(
					'work_status' => 0,
				);
				$workflow_work->where('work_id=%d',$id['work_id'])->save($data_work);
				$data3 = $workflow_work->where('work_id=%d',$id['work_id'])->find();
				$data4 = array(
					'title' => $data3['work_name'],
					'content' => $data3['work_name']."  工作流程于  ".$time." 被  ".session('name')." 办理，流程到此结束，请注意查看",
					'url' => 'http://'.$_SERVER['HTTP_HOST'].__APP__.'/WorkFlow/work_select',
					'time' => $time,
					'status' => '1',
					'user_name' => $data3['work_user_name']
				);
				$information->add($data4);
//				$this->systemlog("办理", "工作流程", "办理工作流程---".$data3['work_name']);
//				$this->redirect('work_list');
				$this->success('办理成功，流程结束','work_list');
				break;exit();
			}
			if($a == 1){
	 			break;
	 		}
		}
		if($a == 1){//当所有条件都成立时
			$data_prcs = $workflow_work_prcs->where('work_id='.$id['work_id'].' AND proc_no='.$data_proc['proc_to'])->find();
			if($data_prcs['proc_user'] != null){
				$proc_user = explode(",", $data_prcs['proc_user']);
				foreach ($proc_user as $key=>$value){
					$data_user = $user->where("user_name='$value'")->find();
					$data_dept = $dept->where('id=%d',$data_user['dept_id'])->find();
					$proc_user_selected[$key]['user_name'] = $value;
					$proc_user_selected[$key]['name'] = $data_user['name'];
					$proc_user_selected[$key]['user_name'] = $data_dept['dept_name'];
				}
			}else{
				$proc_user = '';
				$proc_user_selected = '';
			}
			$data_proc1 = $workflow_proc->where('proc_no='.$data_proc['proc_to'].' AND type_id='.$id['type_id'])->find();//查询出下一步的内容
			//当下一步是第二步，并且经办人员为空时，表示需要直接负责人审核
			$user_name = session('user_name');
			if($data_proc['proc_to'] == 2 && empty($data_proc1['proc_user'])){
				$data_user4 = $user->where("user_name='$user_name'")->find();
				if(empty($data_user4['dept_aid_id'])){//当辅助部门为空时
					$sql = "select dept.dept_header, dept.dept_level_header from oa_sys_dept dept, oa_sys_user user where user.user_name='$user_name' and user.dept_id=dept.id";
					$model = M();
					$data_user2 = $model->query($sql);
					if($data_user2[0]['dept_header'] == $user_name){//当部门负责人是当前登录用户时，直接负责人是上级部门领导
						if(!empty($data_user2[0]['dept_level_header'])){
							$data_user3[] = $data_user2[0]['dept_level_header'];
							if( $data_user2[0]['dept_level_header'] != 'peijun' ) 
								$data_user3[] = 'peijun';
						}else{//上级部门领导为空的时候，选择CEO作为直接负责人
							$data_user3[] = 'peijun';
						}
					}else{
						$data_user3[0] = $data_user2[0]['dept_header'];
						if(!empty($data_user2[0]['dept_level_header'])){
							$data_user3[1] = $data_user2[0]['dept_level_header'];
						}
					}
				}else{//辅助部门不为空时，查询出所在部门和辅助部门的部门负责人
					$dept_id = $data_user4['dept_id'].','.$data_user4['dept_aid_id'];
					$dept_id2 = array_unique(explode(',', $dept_id));//去掉重复部门
					$sql = "select dept_header, dept_level_header from oa_sys_dept dept where ";
					foreach($dept_id2 as $keys=>$values){
						if($keys<(count($dept_id2)-1)){
							$sql .= "id=$values or ";
						}else{
							$sql .= "id=$values ";
						}
					}
//					echo $sql;
					$model = M();
					$data_user2 = $model->query($sql);
//					dump($data_user2);exit;
					foreach ($data_user2 as $keys2=>$values2){
						if(empty($values2['dept_level_header'])){
							$data_user3 .= $values2['dept_header'].',';
						}else{
							$data_user3 .= $values2['dept_header'].','.$values2['dept_level_header'].',';
						}
					}
					$data_user3 = substr($data_user3, 0, -1);
					$data_user3 = array_unique(explode(',', $data_user3));
					foreach ($data_user3 as $values3){
						if($values3 != $user_name){
							$data_user3_bak[] = $values3;
						}
					}
					$data_user3 = $data_user3_bak;
				}
				foreach ($data_user3 as $key4=>$value4){
					$data_user1 = $user->where("user_name='$value4'")->find();
					$data_dept1 = $dept->where('id=%d',$data_user1['dept_id'])->find();
					$proc_user_select[$key4]['user_name'] = $value4;
					$proc_user_select[$key4]['name'] = $data_user1['name'];
					$proc_user_select[$key4]['dept_name'] = $data_dept1['dept_name'];
				}
				
//				dump($data_user3);
				
			}elseif($data_proc1['proc_user'] != null){
				$proc_user1 = explode(",", $data_proc1['proc_user']);
				foreach ($proc_user1 as $key1=>$value1){
					if($proc_user != null){
						if(!in_array($value1, $proc_user)){
							$data_user1 = $user->where("user_name='$value1'")->find();
							$data_dept1 = $dept->where('id=%d',$data_user1['dept_id'])->find();
							$proc_user_select[$key1]['user_name'] = $value1;
							$proc_user_select[$key1]['name'] = $data_user1['name'];
							$proc_user_select[$key1]['dept_name'] = $data_dept1['dept_name'];
						}
					}else{
						$data_user1 = $user->where("user_name='$value1'")->find();
						$data_dept1 = $dept->where('id=%d',$data_user1['dept_id'])->find();
	//					echo $dept->getLastSql();
	//					dump($data_dept1);
						$proc_user_select[$key1]['user_name'] = $value1;
						$proc_user_select[$key1]['name'] = $data_user1['name'];
						$proc_user_select[$key1]['dept_name'] = $data_dept1['dept_name'];
					}
				}
			}else{
				$proc_user_select = '';
				echo "<script language='javascript' >alert('经办人员为空，请联系管理员设置经办人员!')</script>";
			}
		}
		$work_name = $workflow_work->where('work_id=%d',$id['work_id'])->getField("work_name");
		$data_type = $workflow_type->where('type_id='.$id['type_id'])->find();
		$this->assign('proc_user_selected',$proc_user_selected);
		$this->assign('proc_user_select',$proc_user_select);
	 	$this->assign('data_proc',$data_proc);
	 	$this->assign('name',session('name'));
	 	$this->assign('data_proc1',$data_proc1);
	 	$this->assign('data_type',$data_type);
	 	$this->assign('id',$id);
	 	$this->assign('work_name',$work_name);
	 	$this->display();
	}
	
	public function work_next_ok(){
		$time = date('Y-m-d H:i:s');
		
		$id = array(
			'prcs_id' => I('post.prcs_id'),
			'proc_id' => I('post.proc_id'),
			'type_id' => I('post.type_id'),
			'work_id' => I('post.work_id'),
		);
	 	if(!is_id($id['prcs_id']) || !is_id($id['proc_id']) || !is_id($id['type_id']) || !is_id($id['work_id'])){ $this->error("非法参数！"); }
		
		$workflow_form = M("workflow_form");//流程表单表
	 	$workflow_type = M("workflow_type");//工作流程表
	 	$workflow_work = M('workflow_work');//工作流表
	 	$workflow_proc = M("workflow_process");//流程步骤表
		$workflow_work_prcs = M('workflow_work_prcs');//工作流状态表
		$workflow_work_data = M('workflow_work_data');//存储工作流的值
		$information = M("information");//信息提示
		
		$data = array(
			'user_name' => session('user_name'),
			'prcs_time' => $time
		);
		$workflow_work_prcs->where('prcs_id=%d',$id['prcs_id'])->save($data);//添加操作日志
		//生成下一条记录
		$proc_user = $_POST['proc_user'];//获取办理人员
		$data1 = array(
			'work_id' => $id['work_id'],
			'proc_no' => $_POST['proc_no'],
			'proc_user' => $proc_user,
			'prcs_flag' => 1
		);
		$work_name = $workflow_work->where('work_id=%d',$id['work_id'])->getField('work_name');
		$data2 = array(
			'title' => $work_name,
			'content' => $work_name."  工作流程于  ".$time." 已到您这，请尽快办理。",
			'url' => 'http://'.$_SERVER['HTTP_HOST'].__APP__.'/WorkFlow/work_list',
			'time' => $time,
			'status' => '1',
		);
		$success = $workflow_work_prcs->add($data1);
		if($success){
			$data2['user_name'] = $proc_user;
			$information->add($data2);
	 		$this->systemlog('办理', '工作流', '办理工作流---'.$work_name);
	 		$this->success('办理成功！','work_list');
	 	}else{
	 		$this->error('办理失败！');
	 	}
	}
	
	public function work_proc_user($data_proc,$work_id,$proc_user){//经办人员
		$time = date('Y-m-d H:i:s');
		$workflow_proc = M("workflow_process");//流程步骤表
		$workflow_work_prcs = M('workflow_work_prcs');//工作流状态表
		
		if($data_proc['proc_user']==null || $data_proc['proc_user']==''){//当经办人员为空
			$this->error('暂无经办人员，请联系管理员设置经办人员');
		}else{
			$data_prcs1 = array(
				'work_id' => $work_id,
				'proc_no' => $data_proc['proc_no'],
				'prcs_flag' => '1',
				'proc_user' => $proc_user
			);
			$workflow_work_prcs->add($data_prcs1);
			$this->redirect('work_list');
		}
	}
	
	public function work_proc_dept($data_proc,$work_id){//经办部门
		$time = date('Y-m-d H:i:s');
		
		$workflow_proc = M("workflow_process");//流程步骤表
		$workflow_work_prcs = M('workflow_work_prcs');//工作流状态表
		
		if($data_proc['proc_user']==null || $data_proc['proc_user']==''){//当经办人员为空
			$this->work_proc_dept($data_proc,$work_id);
		}else{
			$proc_user = explode(",", $data_proc['proc_user']);
			foreach ($proc_user as $user){
				$data_prcs1 = array(
					'work_id' => $work_id,
					'user_name' => $user,
					'prcs_time' => $time,
					'proc_no' => $data_proc['proc_no'],
					'prcs_flag' => '1'
				);
				$workflow_work_prcs->add($data_prcs1);
			}
			$this->redirect('work_list');
		}
	}
	
	public function work_proc_priv(){//经办角色
		
	}
	
	public function work_select(){//工作查询
		$this->display();
	}
	
	public function work_type_list(){//按流程分类查询
		$workflow_type = M("workflow_type");
	 	$workflow_sort = M("workflow_sort");
	 	$data_sort = $workflow_sort->order('sort_no')->select();
	 	$data = array();
	 	foreach ($data_sort as $key=>$value){
	 		$sort_id = $value['sort_id'];
	 		$data_type = $workflow_type->where('sort_id='.$sort_id." and status = 1")->select();
	 		foreach ($data_type as $key=>$value2){
	 			$value['one'][$key] = $value2;
	 		}
	 		$data[] = $value;
	 	}
	 	$this->assign('data',$data);
	 	$this->display();
	}
	
	public function work_select_list(){//查询结果列表
		$workflow_work = M("workflow_work");
		$workflow_prcs = M("workflow_work_prcs");
		$workflow_type = M("workflow_type");
		
		$type_id = I('get.type_id')?I('get.type_id'):I('post.type_id');
		if($type_id == '') $type_id = 1;
	 	if(!is_id($type_id)){ $this->error("非法参数！"); }
		
		$user_name = session('user_name');
		$type_name = $workflow_type->where('type_id=%d',$type_id)->getField('type_name');
		$time = date('Y-m');
		$start_time = $_POST['start_time'];
		$start_time = $start_time?$start_time:$time;
		if(!is_ym($start_time)){ $this->error("非法日期时间"); }
		
		$data_work1 = $workflow_work->where("type_id = $type_id AND work_user_name != '$user_name' and date_format(work_time,'%Y-%m')='".$start_time."'")->order('work_id desc')->select();
		if($data_work1 != null){
			foreach ($data_work1 as $value1){
				$work_id = $value1['work_id'];
				$data_prcs2 = $workflow_prcs->where("work_id=$work_id AND user_name='$user_name'")->find();
				if($data_prcs2 !=0){
					$data_prcs3 = $workflow_prcs->where('work_id='.$work_id.' AND proc_no=0')->find();
					if($data_prcs3 != null){
						$data_list[] = array(
							'type_id' => $type_id,
							'work_id' => $value1['work_id'],
							'work_name' => $value1['work_name'],
							'proc_no' => '0'
						);
					}else{
						$data_list[] = array(
							'type_id' => $type_id,
							'work_id' => $value1['work_id'],
							'work_name' => $value1['work_name'],
							'proc_no' => '1'
						);
					}
				}
			}
		}
		$this->assign('start_time',$start_time);
		$this->assign('type_id',$type_id);
		$this->assign('type_name',$type_name);
		$this->assign('data_list',$data_list);
		$this->assign('empty',emptyhtml("list"));
		$this->display();
	}
	
	public function mywork(){//工作查询
		$this->display();
	}
	
	public function mywork_type_list(){//按流程分类查询
		$workflow_type = M("workflow_type");
	 	$workflow_sort = M("workflow_sort");
	 	$data_sort = $workflow_sort->order('sort_no')->select();
	 	$data = array();
	 	foreach ($data_sort as $key=>$value){
	 		$sort_id = $value['sort_id'];
	 		$data_type = $workflow_type->where('sort_id='.$sort_id." and status = 1")->select();
	 		foreach ($data_type as $key=>$value2){
	 			$value['one'][$key] = $value2;
	 		}
	 		$data[] = $value;
	 	}
	 	$this->assign('data',$data);
	 	$this->display();
	}
	
	public function mywork_list(){//查询结果列表
		$workflow_work = M("workflow_work");
		$workflow_prcs = M("workflow_work_prcs");
		$workflow_type = M("workflow_type");
		
		$type_id = I('get.type_id')?I('get.type_id'):1;	
	 	if(!is_id($type_id)){ $this->error("非法参数！"); }
		$user_name = session('user_name');
		$type_name = $workflow_type->where('type_id=%d',$type_id)->getField('type_name');
		$count = $workflow_work->where("work_user_name = '$user_name' AND type_id=$type_id")->order('work_id desc')->count();//查询自己申请的流
		$page = new \Think\Page($count,C("PAGE_LIST_ROWS"));
    	$show = $page->show();//分页输出显示
		$data_work = $workflow_work->where("work_user_name = '$user_name' AND type_id=$type_id")->order('work_id desc')->limit($page->firstRow.','.$page->listRows)->select();//查询自己申请的流程
		if($data_work != null){
			foreach ($data_work as $value){
				$data_prcs = $workflow_prcs->where('work_id='.$value['work_id'].' AND proc_no=0')->find();//查询是否有结束的流
				if($data_prcs != null){
					$data_list[] = array(
						'type_id' => $type_id,
						'work_id' => $value['work_id'],
						'work_name' => $value['work_name'],
						'proc_no' => $data_prcs['proc_no']
					);
				}else{//如果没有
					$data_prcs1 = $workflow_prcs->where('work_id='.$value['work_id'])->order('prcs_id desc')->find();
					$data_list[] = array(
						'type_id' => $type_id,
						'work_id' => $value['work_id'],
						'work_name' => $value['work_name'],
						'proc_no' => $data_prcs1['proc_no']
					);
				}
			}
		}
		
		$this->assign('page',$show);
		$this->assign('type_id',$type_id);
		$this->assign('type_name',$type_name);
		$this->assign('data_list',$data_list);
		$this->assign('empty',emptyhtml("list"));
		$this->display();
	}
	
	public function work_delete(){
		$type_id = I('get.type_id');	
		$work_id = I('get.work_id');	
	 	if(!is_id($type_id) || !is_id($work_id)){ $this->error("非法参数！"); }
		$workflow_work = M("workflow_work");
		$workflow_work_prcs = M('workflow_work_prcs');//工作流状态表
		$workflow_work_data = M('workflow_work_data');//存储工作流的值
		
		$work_name = $workflow_work->where('work_id=%d',$work_id)->getField('work_name');
		//删除该工作流的所有记录
		$success = $workflow_work->where('work_id=%d',$work_id)->delete();
		$workflow_work_prcs->where('work_id=%d',$work_id)->delete();
		$workflow_work_data->where('work_id=%d',$work_id)->delete();
		if($success){
	 		$this->systemlog('delete', '工作流程', '删除工作流---'.$work_name);
	 		$this->ajaxReturn('1');
	 	}else{
	 		$this->ajaxReturn('0');
	 	}
	}
	
	public function work_proc_view(){
		$type_id = I('get.type_id');	
		$work_id = I('get.work_id');	
	 	if(!is_id($type_id) || !is_id($work_id)){ $this->error("非法参数！"); }
		
		$user = M("sys_user");
	 	$workflow_type = M("workflow_type");//工作流程表
	 	$workflow_work = M('workflow_work');//工作流表
	 	$workflow_proc = M("workflow_process");//流程步骤表
		$workflow_work_prcs = M('workflow_work_prcs');//工作流状态表
		
		$work_name = $workflow_work->where('work_id=%d',$work_id)->getField('work_name');
		$type_name = $workflow_type->where('type_id=%d',$type_id)->getField('type_name');
		$data_prcs = $workflow_work_prcs->where('work_id=%d',$work_id)->select();
		foreach ($data_prcs as $key=>$value){
			if($value['proc_no'] != 0){
				$data_proc = $workflow_proc->where('type_id='.$type_id.' AND proc_no='.$value['proc_no'])->find();
				if($value['user_name'] != null){
					$name = $user->where("user_name = '".$value['user_name']."'")->getField('name');
				}else{
					$name = '';
				}
				$data[$key]['proc_no'] = $value['proc_no'];
				$data[$key]['proc_name'] = $data_proc['proc_name'];
				$data[$key]['name'] = $name;
				$data[$key]['prcs_time'] = $value['prcs_time'];
				$status = 1;
			}else{
				$status = 0;
			}
		}
		$this->assign('type_name',$type_name);
		$this->assign('work_name',$work_name);
		$this->assign('data',$data);
		$this->assign('status',$status);
		$this->display();
	}
	
	public function work_form_look(){
		$type_id = I('get.type_id');	
		$work_id = I('get.work_id');
		$workflow_work = M("workflow_work");
		$workflow_type = M("workflow_type");//工作流程表
		$workflow_form = M("workflow_form");//流程表单表
		$workflow_work_prcs = M('workflow_work_prcs');//工作流状态表
		$workflow_work_data = M('workflow_work_data');//存储工作流的值
		$workflow_proc = M("workflow_process");//流程步骤表
		$form_id = $workflow_type->where('type_id=%d',$type_id)->getField('form_id');
		$form_model = $workflow_form->where('form_id=%d',$form_id)->getField('form_model');//查询表单模型
		$form_model1 = html_element($form_model);
		$data_value = $workflow_work_data->where('work_id=%d',$work_id)->getField('data_value');
		$data_value = explode('***', $data_value);
		foreach ($form_model1 as $key=>$value){
 			$data_bak1 = array(
 				$value => '<font color=red>'.$data_value[$key].'</font>',
 			);
 			$form_model = strtr($form_model,$data_bak1);
	 	}
	 	$title = $workflow_work->where('work_id=%d',$work_id)->getField('work_name');
	 	$this->assign('title',$title);
		$this->assign('data',$form_model);
		$this->display();
	}
	
	private function replace($array_str,$array_rep,$str){
		$pattern_array=array();
		$replace_array=array();
		$index=1;
		$i=0;
		
		/**
	 	 * 查询个人信息  start
	 	 */
		$time = date('Y-m-d');
		$name = session('name');
		$user_name = session('user_name');
		$user = M("sys_user");
		$dept = M("sys_dept");
		
		$data_user = $user->where("user_name = '$user_name'")->find();
		$job = $data_user['job'];
		$dept_name = $dept->where('id='.$data_user['dept_id'])->getField('dept_name');
		/**end***/
		
		//当替代数组为空时候
		if(count($array_rep)==0){
			$model = html_element($str);
			$session_value=session('wpsqqd');
//			echo $str;exit;
			foreach ($model as $key=>$value){
		 		$title = explode(" ",strstr($value, 'title='));
		 		$form_item = substr($title[0], '6');//表title的值
	 			if($form_item == '填表日期' || $form_item == '填单日期'){
	 				$tmp_str=preg_replace(array("/name=data_(\d{0,})/","/>/"),array("name=data_".$index++," onfocus=this.blur() value=".$time." >"),$value);
					$str=str_replace($value,$tmp_str,$str);
	 			}elseif($form_item == '姓名'){
	 				$tmp_str=preg_replace(array("/name=data_(\d{0,})/","/>/"),array("name=data_".$index++," value=".$name." readonly >"),$value);
					$str=str_replace($value,$tmp_str,$str);
	 			}elseif($form_item == '部门'){
	 				$tmp_str=preg_replace(array("/name=data_(\d{0,})/","/>/"),array("name=data_".$index++," value=".$dept_name." readonly >"),$value);
					$str=str_replace($value,$tmp_str,$str);
	 			}elseif($form_item == '职务' || $form_item == '职位'){
	 				$tmp_str=preg_replace(array("/name=data_(\d{0,})/","/>/"),array("name=data_".$index++," value=".$job." readonly >"),$value);
					$str=str_replace($value,$tmp_str,$str);
	 			}else if(isset($session_value)&&preg_match("/title=(物品申请清单|\'物品申请清单\'|\"物品申请清单\")/",$value)){
					$tmp_str=preg_replace(array("/name=data_(\d{0,})/","/<\/TEXTAREA>/"),array("name=data_".$index++,$session_value."</TEXTAREA>"),$value);
					$str=str_replace($value,$tmp_str,$str);
					$i++;
	 			}else{
	 				$replace_array[]=preg_replace("/name=data_(\d{0,})/","name=data_".$index++,$array_str[$key]);
					$pattern_array[]=$value;
	 			}
		 	}
			$str=str_replace($pattern_array,$replace_array,$str);
			return $str;
		}
		else{
			foreach($array_str as $key=>$value){
				//echo $value;
				if(preg_match("/<INPUT/",$value)){
					$tmp_str=preg_replace(array("/name=data_(\d{0,})/","/>/"),array("name=data_".$index++," value=".$array_rep[$i++]." >"),$value);
					$str=str_replace($value,$tmp_str,$str);
				}
				else if(preg_match("/<TEXTAREA/",$value)){
					$tmp_str=preg_replace(array("/name=data_(\d{0,})/","/<\/TEXTAREA>/"),array("name=data_".$index++,$array_rep[$i++]."</TEXTAREA>"),$value);
					$str=str_replace($value,$tmp_str,$str);
				}
				else if(preg_match("/<SELECT/",$value)){
					//去selectd
					$tmp_str=str_replace("selected","",$value);
					//加selected
					$v=$array_rep[$i++];
					preg_match("/<OPTION\s{0,} value=".$v.">"."/",$tmp_str,$m);
					$tmp_str=str_replace($m[0],"<OPTION  selected value=".$v.">",$tmp_str);	
					$tmp_str=preg_replace("/name=data_(\d{0,})/","name=data_".$index++,$tmp_str);
					$str=str_replace($value,$tmp_str,$str);
				}
				else{
					echo "没有匹配到";
				}
			}
			return $str;
		}
	}
	
	public function work_search(){//工作流监控
		$this->display();
	}
	
	public function work_search_type_list(){
		$workflow_type = M("workflow_type");
	 	$workflow_sort = M("workflow_sort");
	 	$data_sort = $workflow_sort->order('sort_no')->select();
	 	$data = array();
	 	foreach ($data_sort as $key=>$value){
	 		$sort_id = $value['sort_id'];
	 		$data_type = $workflow_type->where('sort_id='.$sort_id." and status = 1")->select();
	 		foreach ($data_type as $key=>$value2){
	 			$value['one'][$key] = $value2;
	 		}
	 		$data[] = $value;
	 	}
	 	$this->assign('data',$data);
	 	$this->display();
	}
	
	public function work_search_list(){
		$workflow_work = M("workflow_work");
		$workflow_prcs = M("workflow_work_prcs");
		$workflow_type = M("workflow_type");
		$type_id = I('get.type_id')?I('get.type_id'):I('post.type_id');
		if($type_id == ''){ $type_id = 1; }	
	 	if(!is_id($type_id)){ $this->error("非法参数！"); }
		
		$datetime = I('post.datetime') ? I('post.datetime') : date("Y-m");
		if(!is_ym($datetime)){ $this->error("非法日期时间"); }
		
		$model = M();
		$sql = "select work.*,prcs.proc_no from oa_workflow_work work, oa_workflow_work_prcs prcs where work.type_id=$type_id and work.work_id=prcs.work_id and date_format(work.work_time,'%Y-%m')='$datetime' order by prcs.prcs_id desc";
		$data = $model->query($sql);
//		$count = count($data);
//		$page = new \Think\Page($count,C("PAGE_LIST_ROWS"));
//		$show = $page->show();//分页输出显示
//		$sql .=" limit ".$page->firstRow.",".$page->listRows;
		$data = $model->query($sql);
		$work_id = array();
		foreach ($data as $key=>$value){
			if(!in_array($value['work_id'], $work_id)){
				$work_id[] = $value['work_id'];
				$data_list[] = $value;
			}
		}
		$type_name = $workflow_type->where('type_id=%d',$type_id)->getField('type_name');
		$this->assign('datetime',$datetime);
		$this->assign('type_id',$type_id);
		$this->assign('type_name',$type_name);
		$this->assign('data_list',$data_list); 
		$this->assign('empty',emptyhtml("list")); 
		$this->display();
	}
    
    public function _empty($name){
    	$this->emptyaction($name);
    }
}
?>